<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <h2 class="all">全部商品分类</h2>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
      <div class="sort">
        <div
          class="all-sort-list2"
          @mouseenter="isMouseInCategory = true"
          @mouseleave="mouseLeaveCategory"
        >
          <div
            class="item"
            v-for="(category1, index) in category1List"
            :key="category1.id"
            @mouseenter="mouseEnterItemThrottle(index, category1.id)"
            :class="{ active: index === mouseEnterIndex }"
          >
            <h3>
              <a>{{ category1.name }}</a>
            </h3>
            <div class="item-list clearfix">
              <div class="subitem">
                <dl
                  class="fore"
                  v-for="category2 in category1.children"
                  :key="category2.id"
                >
                  <dt>
                    <a>{{ category2.name }}</a>
                  </dt>
                  <dd>
                    <em>
                      <a href="">婚恋/两性</a>
                    </em>
                  </dd>
                  <dd>
                    <em>
                      <a href="">婚恋/两性</a>
                    </em>
                  </dd>
                </dl>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import { throttle } from "lodash";
export default {
  name: "TypeNav",
  data() {
    return {
      /*
        定义一个数据mouseEnterIndex保存当前鼠标移入三级列表的下标
        在item列表中进行判断，如果当前item的下标和mouseEnterIndex相等，则添加active类名
        当鼠标移出整个三级列表区域的时候,把这个mouseEnterIndex的值恢复为-1
      */
      mouseEnterIndex: -1,

      /* 
        因为mouseEnterItem事件函数节流后,鼠标移出整个三级列表区域,还会再执行一次
        需要一个数据判断鼠标是否还在三级列表区域
        如果不在,则mouseEnterItem事件函数中的核心代码就不再执行了
      */
      isMouseInCategory: false,
    };
  },
  mounted() {
    //1.初始化请求一级分类列表
    this.getCategory1List();

    //2.使用lodash的throttle函数对mouseEnterItem进行节流处理
    this.mouseEnterItemThrottle = throttle(this.mouseEnterItem, 100, {
      leading: false, // 指定调用在节流开始前是否执行一次函数
      trailing: true, //指定在调用节流后是否执行一次函数(如果为true,鼠标移出后时间到了还会再执行一次函数;如果为false,鼠标在节流时间没到的时候移入某个元素上,函数就会一直不再触发)
    });
  },
  computed: {
    ...mapState("Category", ["category1List"]),
  },
  methods: {
    ...mapActions("Category", ["getCategory1List", "getCategory2List"]),

    //1.鼠标移入三级分类某个列表的事件回调函数
    mouseEnterItem(index, id) {
      //判断当前鼠标是否还在三级列表区域,如果不在了,则后边的代码不再执行
      if (!this.isMouseInCategory) return;

      //修改控制动态样式元素的下标
      this.mouseEnterIndex = index;
      //调用当前一级分类属性的二级分类列表请求
      this.getCategory2List(id);
    },

    //2. 定义一个函数占位,用来收集throttle处理的节流函数
    mouseEnterItemThrottle() {},

    //3. 鼠标离开整个三级分类区域的事件回调函数
    mouseLeaveCategory() {
      //把保存鼠标所在三级列表的下标恢复为-1
      this.mouseEnterIndex = -1;

      //把保存鼠标是否在三级列表中的isMouseInCategory变成false
      this.isMouseInCategory = false;
    },
  },
};
</script> 

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          /* &:hover {
            .item-list {
              display: block;
            }
          } */

          &.active {
            h3 {
              background: yellowgreen;
            }
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>
